import jwtDecode, { JwtPayload } from 'jwt-decode';

export function is_token_valid(token?: string): boolean {
  if (!token) {
    return false;
  }
  const now = Date.now() / 1000;
  const jwt = jwtDecode<JwtPayload>(token);
  if (!jwt.exp) {
    return true;
  }
  if (now > jwt.exp - 60 * 50) {
    return false;
  }
  return true;
}

export function get_access_token(): string {
  return localStorage.getItem('access_token') as string
}

export function get_refresh_token(): string {
  return localStorage.getItem('refresh_token') as string
}

export function set_access_token(access_token: string) {
  localStorage.setItem('access_token', access_token)
}

export function set_refresh_token(refresh_token: string) {
  localStorage.setItem('refresh_token', refresh_token)
}

export function set_username(username: string){
  localStorage.setItem('username', username)
}

export function get_username(username: string){
	localStorage.getItem('username')
  }